package com.pearl.generator.pojo.mapstruct;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.pearl.generator.pojo.entity.DataSourceEntity;
import com.pearl.generator.pojo.request.SaveDataSourceRequest;
import com.pearl.generator.pojo.request.UpdateDataSourceRequest;
import com.pearl.generator.pojo.vo.DataSourceNameVO;
import com.pearl.generator.pojo.vo.DataSourceVO;
import org.mapstruct.Mapper;
import java.util.List;

/**
 * @author TangDan
 * @version 1.0
 * @since 2022/8/4
 */
@Mapper(componentModel = "spring")
public interface DataSourceMapStructMapper {

    default DataSourceVO dataSourceEntity2DataSourceVO(DataSourceEntity dataSourceEntity) {
        DataSourceVO dataSourceVO = new DataSourceVO();
        if (ObjectUtil.isNotNull(dataSourceEntity)) {
            dataSourceVO.setId(dataSourceEntity.getId());
            dataSourceVO.setCreateTime(dataSourceEntity.getCreateTime());
            dataSourceVO.setUpdateTime(dataSourceEntity.getUpdateTime());
            dataSourceVO.setUrl(dataSourceEntity.getUrl());
            dataSourceVO.setUsername(dataSourceEntity.getUsername());
            dataSourceVO.setName(dataSourceEntity.getName());
            dataSourceVO.setPassword(dataSourceEntity.getPassword());
            // 数据库类型
            DataSourceConfig dataSourceConfig = new DataSourceConfig.Builder(dataSourceEntity.getUrl(), dataSourceEntity.getUsername(), dataSourceEntity.getPassword()).build();
            dataSourceVO.setDbTypeName(dataSourceConfig.getDbType().getDb());
        }
        return dataSourceVO;
    }

    List<DataSourceVO> dataSourceEntityList2DataSourceVOList(List<DataSourceEntity> dataSourceEntityList);

    DataSourceEntity saveDataSourceRequest2DataSourceEntity(SaveDataSourceRequest saveDataSourceRequest);

    DataSourceEntity updateDataSourceRequest2DataSourceEntity(UpdateDataSourceRequest updateDataSourceRequest);

    DataSourceNameVO dataSourceEntity2DataSourceNameVO(DataSourceEntity dataSourceEntity);

    List<DataSourceNameVO> dataSourceEntity2DataSourceNameVO(List<DataSourceEntity> dataSourceEntityList);
}
